package com.kejiguan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.SelectSheduleObject;
import com.kejiguan.ui.model.SubSelectSheduleObject;

import java.util.List;

/**
 * Created by YYY on 16/8/16.
 */
public class SelectSheduleObjectAdapter extends RecyclerView.Adapter<SelectSheduleObjectVH> implements View.OnClickListener{

    private Context context;
    private List<SelectSheduleObject> selectSheduleObjects;
    private LayoutInflater layoutInflater;
    private OnAllSelectListener onAllSelectListener;
    private int selectColor,unSelectColor;

    public SelectSheduleObjectAdapter(Context context, List<SelectSheduleObject> selectSheduleObjects)
    {
        this.context=context;
        this.selectSheduleObjects=selectSheduleObjects;
        layoutInflater=LayoutInflater.from(context);
        selectColor=context.getResources().getColor(R.color.text_color2);
        unSelectColor=context.getResources().getColor(R.color.text_color1);
    }

    @Override
    public SelectSheduleObjectVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=layoutInflater.inflate(R.layout.activity_select_timing_shedule_object_item_layout,null);
        return new SelectSheduleObjectVH(itemView);
    }

    @Override
    public void onBindViewHolder(SelectSheduleObjectVH holder, int position) {

        SelectSheduleObject selectSheduleObject=selectSheduleObjects.get(position);

        holder.displayName.setText(selectSheduleObject.getObjectName());
        if (selectSheduleObject.isSelect())
        {
            holder.selectStateView.setImageResource(R.mipmap.kjg_menu_checked);
            holder.displayName.setTextColor(selectColor);
            holder.displayName.setBackgroundResource(R.drawable.kjg_item_display_bg2);
        }else
        {
            holder.selectStateView.setImageResource(R.mipmap.kjg_menu_uncheck);
            holder.displayName.setTextColor(unSelectColor);
            holder.displayName.setBackgroundResource(R.drawable.kjg_item_display_bg1);
        }

        holder.displayAreaView.setTag(R.id.recyclerview_item_position,position);
        holder.displayAreaView.setOnClickListener(this);
        holder.exhibitionList.setAdapter(new SelectSheduleObjectSubAdapterNew(context,selectSheduleObject.getSubSelectSheduleObjects(),SelectSheduleObjectAdapter.this));
    }

    @Override
    public int getItemCount() {
        if (null==selectSheduleObjects)
        {
            return 0;
        }else
        {
            return selectSheduleObjects.size();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.display_area:
                int position= (int) v.getTag(R.id.recyclerview_item_position);
                SelectSheduleObject selectSheduleObject=selectSheduleObjects.get(position);
                if (selectSheduleObject.isSelect())
                {
                    if (null!=selectSheduleObject.getSubSelectSheduleObjects())
                    {
                        for (SubSelectSheduleObject subSelectSheduleObject:selectSheduleObject.getSubSelectSheduleObjects())
                        {
                            subSelectSheduleObject.setSelect(false);
                        }
                    }

                    selectSheduleObject.setSelect(false);
                }else
                {
                    if (null!=selectSheduleObject.getSubSelectSheduleObjects())
                    {
                        for (SubSelectSheduleObject subSelectSheduleObject:selectSheduleObject.getSubSelectSheduleObjects())
                        {
                            subSelectSheduleObject.setSelect(true);
                        }
                    }

                    selectSheduleObject.setSelect(true);
                }
                checkIsAllSelect();
                notifyItemChanged(position);
                break;
            case R.id.exhibition_layout
                    :
                SubSelectSheduleObject subSelectSheduleObject= (SubSelectSheduleObject) v.getTag(R.id.recyclerview_item_data);
                SelectSheduleObject selectSheduleObject1=subSelectSheduleObject.getSelectSheduleObject();
                int position1=selectSheduleObject1.getPosition();

                if (subSelectSheduleObject.isSelect())
                {
                    subSelectSheduleObject.setSelect(false);
                    selectSheduleObject1.setSelect(false);

                }else
                {

                    subSelectSheduleObject.setSelect(true);
                    boolean isAllSelected=true;
                    for (SubSelectSheduleObject subSelectSheduleObject1:selectSheduleObject1.getSubSelectSheduleObjects())
                    {
                        if (!subSelectSheduleObject1.isSelect())
                        {
                            isAllSelected=false;
                            break;
                        }
                    }
                    selectSheduleObject1.setSelect(isAllSelected);
                }
                checkIsAllSelect();
                notifyItemChanged(position1);
                break;
        }
    }


    private void checkIsAllSelect()
    {
        if (null!=onAllSelectListener)
        {
            boolean isAllSelect=true;
            for (SelectSheduleObject selectSheduleObject:selectSheduleObjects)
            {
                if (!selectSheduleObject.isSelect())
                {
                    isAllSelect=false;
                    break;
                }
            }

            onAllSelectListener.isAllSelect(isAllSelect);
        }
    }

    public void setOnAllSelectListener(OnAllSelectListener onAllSelectListener) {
        this.onAllSelectListener = onAllSelectListener;
    }

    public interface OnAllSelectListener
    {
        public void isAllSelect(boolean isAllSelect);
    }
}
